const { ipcRenderer, contextBridge} = require('electron');

//Callback
function init() {
  console.log('init send: ');
  ipcRenderer.send('init');
}
function terminal(data) {
  console.log('terminal send: ', data);
  ipcRenderer.send('terminal', data);
}
function stdin(localTerm, data) {
  console.log('stdin send: ', data);
  if(localTerm){
    ipcRenderer.send('localStdin', data);
  }else{
    ipcRenderer.send('stdin', data);
  }
}
function onTerminal(listener) {
  console.log('onTerminal renderer send: ', listener);
  ipcRenderer.on('onTerminal', listener);
}
function rendererToMainNoReturnValue1(arg1, arg2) {
  console.log('rendererToMainNoReturnValue1 renderer send: ', arg1, arg2);
  ipcRenderer.send('rendererToMainNoReturnValue1', arg1, arg2);
}

function rendererToMainWithReturnValue1(arg1, arg2) {
  console.log('========rendererToMainWithReturnValue1 renderer send: ', arg1, arg2);
  ipcRenderer.send('rendererToMainWithReturnValue1', arg1, arg2);
}
function onRendererToMainWithReturnValue1(listener) {
  ipcRenderer.on('onRendererToMainWithReturnValue1', listener);
}

function rendererToMainThenMainToRenderer1(arg1, arg2) {
  console.log('rendererToMainThenMainToRenderer1 renderer send: ', arg1, arg2);
  ipcRenderer.send('rendererToMainThenMainToRenderer1', arg1, arg2);
}
function onRendererToMainThenMainToRenderer1(listener) {
  ipcRenderer.on('onRendererToMainThenMainToRenderer1', listener);
}

function rendererToRenderer(winId,arg1) {
  ipcRenderer.sendTo(winId,'onRendererToRenderer',arg1)
}

function onRendererToRenderer(listener) {
  ipcRenderer.on('onRendererToRenderer', listener);
}

//Promise
function rendererToMainNoReturnValue2(arg1, arg2) {
  console.log('rendererToMainNoReturnValue2 renderer invoke: ', arg1, arg2);
  ipcRenderer.invoke('rendererToMainNoReturnValue2', arg1, arg2);
}

function rendererToMainWithReturnValue2(arg1, arg2) {
  console.log('rendererToMainWithReturnValue2 renderer invoke: ', arg1, arg2);
  return ipcRenderer.invoke('rendererToMainWithReturnValue2', arg1, arg2);
}

function getWebContentsId() {
  console.log('getWebContentsId renderer invoke ');
  return ipcRenderer.invoke('getWebContentsId');
}


contextBridge.exposeInMainWorld('ipc', {
  init,
  terminal,
  stdin,
  onTerminal,
  rendererToMainNoReturnValue1,
  rendererToMainWithReturnValue1,
  onRendererToMainWithReturnValue1,
  rendererToMainThenMainToRenderer1,
  onRendererToMainThenMainToRenderer1,
  rendererToMainNoReturnValue2,
  rendererToMainWithReturnValue2,
  getWebContentsId,
  rendererToRenderer,
  onRendererToRenderer
});
